package cn.wolfcode.luowowo.service.impl;


import cn.wolfcode.luowowo.domain.Travel;

import cn.wolfcode.luowowo.query.TravelCondition;
import cn.wolfcode.luowowo.query.TravelObjcet;
import cn.wolfcode.luowowo.repository.TravelRepository;

import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.ITravelService;

import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelRepository repository;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private IDestinationService destinationService;
    @Override
    public Travel get(String id) {
        Optional<Travel> optional = repository.findById(id);
        if (optional.isPresent()){
            Travel travel = optional.get();
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            travel.setDest(destinationService.get(travel.getDestId()));
            return travel;
        }
        return null;
    }

    @Override
    public List<Travel> list() {
        List<Travel> list = repository.findAll();
        return list;
    }



    @Override
    public Page<Travel> query(TravelObjcet qo) {
        //创建查询对象
        Query query = new Query();
        //旅游天数
        TravelCondition travelCondition = qo.getDay();
        if(travelCondition !=null){
            query.addCriteria(Criteria.where("day").gte(travelCondition.getMin()).lte(travelCondition.getMax()));
        }
        //人均消费
        TravelCondition expend = qo.getExpend();
        if (expend != null){
            query.addCriteria(Criteria.where("perExpend").gte(expend.getMin()).lte(expend.getMax()));
        }
        //最新游记
        //找出创建时间最新的
        if(qo.getOrderType() == 1){
            qo.setPageable(PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "createTime"));
        }else if(qo.getOrderType() == 2){
           qo.setPageable(PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "viewnum"));

        }
        if (StringUtils.hasLength(qo.getDestId())){
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }
      //  PageRequest request = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        Page<Travel> page = DBHelper.query(template, Travel.class, query, qo.getPageable());
        List<Travel> travels = page.getContent();
        for (Travel travel : travels) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));

        }

        return page;
    }


    @Override
    public void saveOrUpdate(Travel travel) {

        if(StringUtils.hasLength(travel.getId())){
            Travel travel1=repository.findByDestId(travel.getDestId());
            travel.setDestName(travel1.getDestName());
            //用话ID
            this.save(travel);
        }else{
            this.update(travel);
        }
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public void save(Travel travel) {
        travel.setId(null);
        repository.save(travel);
    }

    @Override
    public void update(Travel travel) {

        repository.save(travel);
    }

    @Override
    public void changeState(String id, int state) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("state",state);
        template.updateMulti(query,update,Travel.class);

    }


}
